package com.ruoyi.web.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.web.domain.SoftwareLink;
import com.ruoyi.web.mapper.SoftwareLinkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.SoftwareVersionMapper;
import com.ruoyi.web.domain.SoftwareVersion;
import com.ruoyi.web.service.ISoftwareVersionService;

/**
 * 软件版本Service业务层处理
 *
 * @author song
 * @date 2024-08-26
 */
@Service
public class SoftwareVersionServiceImpl implements ISoftwareVersionService
{
    @Autowired
    private SoftwareVersionMapper softwareVersionMapper;
    @Autowired
    private SoftwareLinkMapper softwareLinkMapper;

    /**
     * 查询软件版本
     *
     * @param versionId 软件版本主键
     * @return 软件版本
     */
    @Override
    public SoftwareVersion selectSoftwareVersionByVersionId(Long versionId)
    {
        return softwareVersionMapper.selectSoftwareVersionByVersionId(versionId);
    }

    /**
     * 查询软件版本列表
     *
     * @param softwareVersion 软件版本
     * @return 软件版本
     */
    @Override
    public List<SoftwareVersion> selectSoftwareVersionList(SoftwareVersion softwareVersion)
    {
        return softwareVersionMapper.selectSoftwareVersionList(softwareVersion);
    }

    /**
     * 新增软件版本
     *
     * @param softwareVersion 软件版本
     * @return 结果
     */
    @Override
    public int insertSoftwareVersion(SoftwareVersion softwareVersion)
    {
        softwareVersion.setCreateTime(DateUtils.getNowDate());
        softwareVersion.setUpdateTime(DateUtils.getNowDate());
        return softwareVersionMapper.insertSoftwareVersion(softwareVersion);
    }

    /**
     * 修改软件版本
     *
     * @param softwareVersion 软件版本
     * @return 结果
     */
    @Override
    public int updateSoftwareVersion(SoftwareVersion softwareVersion)
    {
        softwareVersion.setUpdateTime(DateUtils.getNowDate());
        return softwareVersionMapper.updateSoftwareVersion(softwareVersion);
    }

    /**
     * 批量删除软件版本
     *
     * @param versionIds 需要删除的软件版本主键
     * @return 结果
     */
    @Override
    public int deleteSoftwareVersionByVersionIds(Long[] versionIds)
    {
        ArrayList<Long> linkIds = new ArrayList<>();
        SoftwareLink softwareLink = null;

        // 查询每个版本对应的链接ID并收集所有链接ID
        for (Long versionId : versionIds) {
            softwareLink = new SoftwareLink();
            softwareLink.setVersionId(versionId);
            List<SoftwareLink> linkList = softwareLinkMapper.selectSoftwareLinkList(softwareLink);

            // 收集所有链接ID
            for (SoftwareLink link : linkList) {
                linkIds.add(link.getLinkId());
            }
        }

        // 批量删除所有收集到的链接
        if (!linkIds.isEmpty()) {
            softwareLinkMapper.deleteSoftwareLinkByLinkIds(linkIds.toArray(new Long[0]));
        }
        // 删除软件对应的版本
        return softwareVersionMapper.deleteSoftwareVersionByVersionIds(versionIds);
    }

    /**
     * 删除软件版本信息
     *
     * @param versionId 软件版本主键
     * @return 结果
     */
    @Override
    public int deleteSoftwareVersionByVersionId(Long versionId)
    {
        return softwareVersionMapper.deleteSoftwareVersionByVersionId(versionId);
    }
}
